﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
namespace WebTeamplate
{
    public partial class Admin_Language : BasePage
    {
        #region Fields
        public string paramater
        {
            get
            {
                object o = ViewState["paramater"];
                if (o != null) return (string)o;
                return string.Empty;
            }
            set { ViewState["paramater"] = value; }
        }
        public string urlDetail = "LanguageDetail.aspx";
        int count = 0;
        #endregion

        #region ViewState
       
      
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnAddNew.ClientID);
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
                
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
        }
        #endregion

        #region Methods
        public void InitTextControl()
        {           
            btnAddNew.Text = Resources.Resource.AddNewButtonText;
            
        }
        private void InitData()
        {           
           
        }
        public void LoadData()
        {
            paramater = this.GetPageParams(FilterFieldType.All);
            //paramater = paramater + "F_rblStatus=" + F_rblStatus.SelectedValue;
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            List<Language> lstResult = LanguageUtil.GetAll();
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            rptData.DataSource = lstResult;
            rptData.DataBind();
           
        }
        private void Delete(string id)
        {
            if (LanguageUtil.Delete(id))
                LoadData();
        }
        public void Publish(string id)
        {

            if (LanguageUtil.Publish(id))
                LoadData();
        }
        
        private void OrderDown(string id)
        {
            if (LanguageUtil.Order(id, CommonFunction.OrderDown))
                LoadData();
        }
        private void OrderUp(string id)
        {

            if (LanguageUtil.Order(id, CommonFunction.OrderUp))
                LoadData();
        }
        #endregion

        #region Events,Handler

        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Language cate = (Language)e.Item.DataItem;
                if (cate == null)
                    return;
                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();

                //ltr = (Literal)e.Item.FindControl("ltrSubCode");
                string url = urlDetail + "?" + paramater + "&id=" + cate.LanguageId + "&act=" + CommonFunction.EdityCommand;
                //ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategoryCode) + "</a>";
                ltr = (Literal)e.Item.FindControl("ltrName");
                ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.Name) + "</a>";
                ImageButton imgButton = (ImageButton)e.Item.FindControl("btnPublish");
                imgButton.CommandArgument = cate.LanguageId.ToString();
                if (cate.Published == CommonFunction.True)
                    imgButton.ImageUrl = CommonFunction.ActiveImage;
                else
                    imgButton.ImageUrl = CommonFunction.DeActiveImage;
                imgButton = (ImageButton)e.Item.FindControl("imgOrderUp");
                if (e.Item.ItemIndex + 1 == count)
                    imgButton.Visible = false;
                else
                {
                    imgButton.ToolTip = Resources.Resource.OrderUpTooltip;
                    imgButton.CommandArgument = cate.LanguageId.ToString();
                    imgButton.ImageUrl = CommonFunction.OrderUpImage;
                }
                imgButton = (ImageButton)e.Item.FindControl("imgOrderDown");
                if (e.Item.ItemIndex == 0)
                    imgButton.Visible = false;
                else
                {
                    imgButton.CommandArgument = cate.LanguageId.ToString();
                    imgButton.ToolTip = Resources.Resource.OrderDownTooltip;
                    imgButton.ImageUrl = CommonFunction.OrderDownImage;
                    imgButton.Visible = true;
                }
                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = url;
                hplEdit.Text = Resources.Resource.EditText;
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.DeleteText;
                lbtnDelete.CommandArgument = cate.LanguageId.ToString();
                hidListId.Value += cate.LanguageId + ";";
            }
        }
        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "publish":
                    {
                        Publish(id);
                        break;
                    }
                case "delete":
                    {
                        Delete(id);
                        break;
                    }
                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
               

            }

        }
     
     
       
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlDetail + "?" + paramater);
        }
     
        
        #endregion
    }
}
